package com.fsnbigdata.repository;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt;
import org.springframework.roo.addon.layers.repository.jpa.annotations.RooJpaRepositoryCustomImpl;
import com.fsnbigdata.domain.Germplasm;
import com.fsnbigdata.domain.Organism;
import com.fsnbigdata.domain.QGermplasm;
import com.querydsl.core.types.Path;
import com.querydsl.jpa.JPQLQuery;
import io.springlets.data.domain.GlobalSearch;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt.AttributeMappingBuilder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * = GermplasmRepositoryImpl
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJpaRepositoryCustomImpl(repository = GermplasmRepositoryCustom.class)
@Transactional(readOnly = true)
public class GermplasmRepositoryImpl extends QueryDslRepositorySupportExt<Germplasm> implements GermplasmRepositoryCustom {

    /**
     * TODO Auto-generated constructor documentation
     */
    GermplasmRepositoryImpl() {
        super(Germplasm.class);
    }

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String OWNER = "owner";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String UNIT = "unit";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String COLLECTOR = "collector";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String COLLECT_DATE = "collectDate";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String COLLECT_NO = "collectNo";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String COLLECT_LOCATION = "collectLocation";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String PRESERVE_NO = "preserveNo";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ORGANISM = "organism";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String RESOURCE_TYPE = "resourceType";

    /**
     * TODO Auto-generated method documentation
     *
     * @param organism
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<Germplasm> findByOrganism(Organism organism, GlobalSearch globalSearch, Pageable pageable) {
        QGermplasm germplasm = QGermplasm.germplasm;
        JPQLQuery<Germplasm> query = from(germplasm);
        Assert.notNull(organism, "organism is required");
        query.where(germplasm.organism.eq(organism));
        Path<?>[] paths = new Path<?>[] { germplasm.organism, germplasm.preserveNo, germplasm.unit, germplasm.owner, germplasm.collectNo, germplasm.collector, germplasm.collectDate, germplasm.collectLocation, germplasm.resourceType };
        applyGlobalSearch(globalSearch, query, paths);
        AttributeMappingBuilder mapping = buildMapper().map(ORGANISM, germplasm.organism).map(PRESERVE_NO, germplasm.preserveNo).map(UNIT, germplasm.unit).map(OWNER, germplasm.owner).map(COLLECT_NO, germplasm.collectNo).map(COLLECTOR, germplasm.collector).map(COLLECT_DATE, germplasm.collectDate).map(COLLECT_LOCATION, germplasm.collectLocation).map(RESOURCE_TYPE, germplasm.resourceType);
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, germplasm);
    }
}
